﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    form > div { margin-top: 20px; }
</style>
</head>
<%
String errMsg = null;
RequestParameter param = new RequestParameter(request);
String loginId = param.getString("loginId", "");

if (request.getMethod().equals("POST")) {
    User user = UserDAO.selectByLoginId(loginId);
    if (user != null) {
        String passwd = param.getString("passwd", "");
        String encryptedPasswd = UserService.encryptPasswd(passwd);
        if (user.getPasswd().equals(encryptedPasswd)) {
            if (user.isEnabled()) {
                UserService.login(session, user);
                String returnUrl = (String)session.getAttribute("returnUrl");
                if (returnUrl == null) returnUrl = "/lecture2a/03_login/index.jsp";
                response.sendRedirect(returnUrl);
                return;
            }
            else errMsg = "비활성화된 사용자입니다";
        }
        else errMsg = "비밀번호가 잘못되었습니다";
    }
    else errMsg = "로그인 아이디가 잘못되었습니다";
}
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>로그인</h1>
    <hr />

    <form method="post">
        <div>아이디</div>
        <input type="text" name="loginId" value="<%= loginId %>" />

        <div>비밀번호</div>
        <input type="password" name="passwd" />

        <div>
            <button type="submit" class="btn btn-primary">
                <i class="icon-ok icon-white"></i>
                로그인
            </button>
            <a class="btn" href="reset.jsp"><i class="icon-search"></i> 비밀번호 찾기</a>
            <a class="btn" href="register.jsp"><i class="icon-user"></i> 회원가입</a>
        </div>
    </form>
    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
